<?php
if (! defined('BASEPATH'))
    exit('No direct script access allowed');
/**
 *
 * Sets up basic model functions. All user created model classes should
 * extend this to gain access to its basic database model functions.
 *
 * @package		yg
 * @subpackage	Models
 * @author		yunxiang
 * @copyright	Copyright (c) 2010, ygkjw.com, Inc.
 * @since		Version 1.0
 */
class Mongo_model extends CI_Model
{
    protected $_table_name = ''; //Table Name
    protected $_insert_id = true; //替换mongodb _id 名
    protected $_ins_data_arr = array();
    
    
    protected $_web_no_key = "key one";   //加密字符串
    protected $_web_no_num = -8;		  //后半部分截取长度
	
    public function __construct()
    {
		$this->load->library("mongo_db");
        parent::__construct();
        
    }
    
    /**
     * 统计符合条件的总数
     * 
     * @param $keys 
     * @param $values
     */
	protected function count($keys = NULL , $values = NULL)
    {
    	if($keys != NULL)
    	{
    		$where_arr = array();
    		if( ! is_array($keys) )
    		{
    			$where_arr[$key] = $values ;
    		}
    		else
    		{
    			$where_arr = $keys;
    		}
    		$this->mongo_db->where($where_arr);
    	}
    	
    	return $this->mongo_db->count($this->_table_name);
    }
    
    /**
     * 获得一行数据
     * 
     * @param $keys
     * @param $values
     * 
     */
	protected function get_once($keys = NULL , $values = NULL)
    {
    	if($keys != NULL)
    	{
    		$where_arr = array();
    		if( ! is_array($keys) )
    		{
    			$where_arr[$key] = $values ;
    		}
    		else
    		{
    			$where_arr = $keys;
    		}
    		$this->mongo_db->where($where_arr);
    	}
    	
    	$date_info = $this->mongo_db->limit(1)->get($this->_table_name);
        //print_r($date_info);
		if(count($date_info) == 1)
		{
            foreach($date_info as $val)
            {
                return $val;
            }
		}
    	return $date_info;
    }
    
    /**
     * 获得多行数据
     * 
     * @param unknown_type $keys
     * @param unknown_type $values
     * 
     */
    protected function get($keys = NULL , $values = NULL)
    {
    	if($keys != NULL)
    	{
    		$where_arr = array();
    		if( ! is_array($keys) )
    		{
    			$where_arr[$key] = $values ;
    		}
    		else
    		{
    			$where_arr = $keys;
    		}
    		$this->mongo_db->where($where_arr);
    	}
    	
    	return $this->mongo_db->get($this->_table_name);
    }
    
    /**
     * 更新程序
     * 
     * @param array/string 如果是数组，则是修改的数组，如果是字符串，配合where，则是key，where是vlaue
     * 
     */
	protected function update($sets = NULL,$wheres = NULL)
	{
		if( $sets != NULL )
		{	
			$sets_arr = array();
			if(! is_array($sets))
			{
				$sets_arr[$sets] = $where;
			}
			else
			{
				$sets_arr = $sets;
			}
			
			$this->mongo_db->set($sets_arr);
		}
		if( $wheres != NULL )
		{
			if(is_array($wheres))
			{
				$this->mongo_db->where($wheres);
			}
		}
		
		return $this->mongo_db->update($this->_table_name);
	}
    
    /**
     * 插入数据
     * 
     * @param array  $date_arr 传入的数组
     * 
     * @return bool 
     * 
     */
    protected function insert( $date_arr = array() )
    {
    	//统一 _id
    	if($this->_insert_id)
    	{
    		$date_arr['_id'] = $this->get_no();
    	}
    	
    	return $this->mongo_db->insert($this->_table_name,$date_arr);
    }
	
    /**
     * 分页
     * 
     * @param $limit 
     */
	protected function set_limit($limit = 0)
    {
        if ($limit != 0)
        {
            $limit_array = explode(",", $limit);
            $rows = intval($limit_array['0']);
            $offset = isset($limit_array['1']) ? intval($limit_array['1']) : 0;
            $this->mongo_db->limit($rows);
			$this->mongo_db->offset($offset);
			return true;
        }
		else
		{
			return false;
		}
    }
    
    
	
    /**
     * 多重选择like
     *
     */
	protected function set_like($like_arr = array(),$keyword = '')
    {
    	if(is_array($like_arr))
    	{
    		foreach($like_arr as $key => $info)
    		{
    			$this->mongo_db->like(trim($key),trim($info));
    		}
    	}
    	else
    	{
    		$this->mongo_db->like(trim($like_arr),trim($keyword));
    	}
    }
    
    /**
     * 插入数据检测不存在加上
     *
     */
    protected function set_data($in_arr = array(), $set_arr = NULL)
    {
    	if(empty($set_arr))
    	{
    		$set_arr = $this->_ins_data_arr;
    	}
    	if(empty($set_arr))
    	{
    		return $in_arr;
    	}
    	
    	foreach($set_arr as $key => $info)
    	{
    		if( ! isset($in_arr[$key]) )
    		{
    			$in_arr[$key] = $info;
    		}
    	}
    	
    	return $in_arr;
    }
	
	/**
	*	--------------------------------------------------------------------------------
	*	//! Where  条件
	*	--------------------------------------------------------------------------------
	*
	*	Get the documents based on these search parameters.  The $wheres array should 
	*	be an associative array with the field as the key and the value as the search
	*	criteria.
	*
	*	@usage : $this->mongo_db->where(array('foo' => 'bar'))->get('foobar');
	*/
	
	public function where($wheres, $value = null)
	{
		return $this->mongo_db->where($wheres,$value);
	}
    
    /**
     * 产生唯一的时间编号
     * 
     * @return string ID
     */
    protected function get_no($str = '')
    {
    	if($str == '')
    	{
    		 $str = $this->_table_name ."_" ;
    	}
    	$id = uniqid($str,true);
		$check_data = substr(md5( $id . $this->input->ip_address() . $this->input->user_agent() . $this->_web_no_key),$this->_web_no_num);
		$id = uniqid($str);
		return $id . '_' . $check_data;
    }

    protected function select($includes = array(), $excludes = array())
    {
        return $this->mongo_db->select($includes,$excludes);
    }

    //@usage : $this->mongo_db->order_by(array('foo' => 'ASC'))->get('foobar');
    protected function order_by($fields = array())
    {
        return $this->mongo_db->order_by($fields);
    }

    protected function limit($x = 99999)
    {
        return $this->mongo_db->limit($x);
    }

    protected function offset($x = 0)
    {
        return $this->mongo_db->offset($x);
    }
}
/* End of file base_model.php */
/* Location: ./system/application/models/base_model.php */
